package com.tnsw.noxnox;

import java.util.ArrayList;
import java.util.Hashtable;

import com.tnsw.coreutils.AsyncManager;
import com.tnsw.noxnox.db.DBManager;
import com.tnsw.noxnox.db.tables.DownloadProgress;
import com.tnsw.noxnox.downloader.DownloaderService;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CatalogAdapter extends BaseAdapter {

	private String tag = "CatalogAdapter";
	private Context context;
	private DBManager dbm;
	private AsyncManager asyncManager;
	private ArrayList<DownloadProgress> a;
	private Hashtable<Integer, CatalogSingleView> hashSingleView;
	private static LayoutInflater inflater=null;
	
	private TextView title;
	private ImageView thumbImage;
	
	public CatalogAdapter(Context context) {
		Log.d(tag, "Constructor(context)");
		this.context = context;
		this.dbm = DBManager.getInstance(context);
		this.asyncManager = AsyncManager.getInstance();
		
		this.a = this.dbm.getDownloadProgress("", null, "order by title desc");
		if (this.a == null) {
			this.asyncManager.appendNetworkLog("table == null");
		} else {
			this.asyncManager.appendNetworkLog("record_count=" + a.size());
		}
		
		this.hashSingleView = new Hashtable<Integer, CatalogSingleView>();
		
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
	}
	
	public void doRefreshBackground() {
		this.a = this.dbm.getDownloadProgress("", null, "order by title desc");
		Log.d(tag, "doRefreshBackgroud()");
	}
	
	public void doRefresh() {
		this.a = this.dbm.getDownloadProgress("", null, "order by title desc");
		this.notifyDataSetChanged();
		//Log.d(tag, "doRefresh()");
	}
	
	@Override
	public int getCount() {
		if (this.a != null) {
			return(this.a.size());
		} else {
			return 0;	
		}
		
	}

	@Override
	public Object getItem(int position) {
		if (this.a == null) {
			return null;	
		} else {
			return(a.get(position));
		}
		
	}

	@Override
	public long getItemId(int position) {
		return(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		DownloaderService ds = DownloaderService.getInstance(this.context);
		
		//Log.d(tag, "getView(position=" + position + ")");
		View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.list_row, null);
			Log.d(tag, "inflate new view(position=" + position + ")");
		} else {
			//return(convertView);
			//Log.d(tag, "Used existing..");
		}
		
		if (this.a != null) {
			DownloadProgress dp = this.a.get(position);
			//Log.d(tag, "position=" + position + ", pk=" + dp.primarykey + ", title=" + dp.title + ", thumb=" + dp.thumbnail_filename);
			
			//this.title = (TextView) vi.findViewById(R.id.title);
			//this.title.setText(dp.title);
			
			//this.thumbImage = (ImageView) vi.findViewById(R.id.list_image);
			//this.thumbImage.setImageBitmap(BitmapFactory.decodeFile(dp.thumbnail_filename));
			
			if (this.hashSingleView.containsKey(dp.primarykey) == false) {
				CatalogSingleView singleView = new CatalogSingleView(this.context, dp, this);
				this.hashSingleView.put(dp.primarykey, singleView);
				Log.d(tag, "Added singleView into hashSingleView");
				vi = singleView.getView();
			} else {
				CatalogSingleView singleView = this.hashSingleView.get(dp.primarykey);
				singleView.refreshDp(dp);
				vi = singleView.getView();
			}
			
			ds.registerDLInterface(dp.primarykey, this.hashSingleView.get(dp.primarykey));
		} else {
			Log.e(tag, "a == null");
		}
		
		return(vi);
	}
	
	

}
